package org.easy.tool.io;

import lombok.extern.slf4j.Slf4j;
import org.easy.tool.consts.EasyConstants;
import org.easy.tool.date.DateUtil;

import java.io.*;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description: 文件工具类
 * @Author: wxl
 * @CreateTime: 2022/12/27 16:10
 */
@Slf4j
public class FileUtil extends cn.hutool.core.io.FileUtil {

    /**
     * 读取resource下的文件
     * @param fileName 文件名
     * @return List<String>
     */
    public static Map<String,String> readAsStringFromResource(String fileName){
        Map<String,String> dataList = new ConcurrentHashMap<>(1 << 8);
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            InputStream is = FileUtil.class.getClassLoader().getResourceAsStream(fileName);
            if (Objects.nonNull(is)) {
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                Properties props = new Properties();
                props.load(br);
                for (Object key : props.keySet()) {
                    Object value = props.getProperty(key.toString());
                    dataList.put((String) key,String.valueOf(value));
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            try {
                if (null != br) {
                    br.close();
                }
                if (null != isr) {
                    isr.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
        return dataList;
    }

    public static void writeToFileFromResource (Map<String,String> data) {
        Properties props = new Properties();
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            String filePath = Objects.requireNonNull(FileUtil.class.getClassLoader().getResource(EasyConstants.TASK_FILE_NAME)).toURI().getPath();
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
            props.load(reader);
            reader.close();

            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
            props.clear();
            if (!data.isEmpty()) {
                data.forEach(props::setProperty);
            }
            props.store(writer,"于时间为:"+ DateUtil.localDateTime() +"更新文件任务完毕!");
            writer.flush();
            writer.close();
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }

    }


}
